package modules.majiang.viewManager
{
	import com.netease.protobuf.UInt64;
	
	import flash.display.MovieClip;
	import flash.events.Event;
	
	import modules.GameEvent;
	import modules.ViewManager;
	import modules.entity.ActionType;
	import modules.majiang.MajiangEvent;
	import modules.majiang.data.TableDataModel;
	import modules.majiang.data.TablePlayerDataModel;
	import modules.majiang.data.TablesDataManager;
	
	import utils.FloatHandler;
	import utils.ResourceManager;

	/**
	 * 玩家信息界面管理
	 * @author xumin.xu 2013-1-21
	 */
	public class PlayerViewManager extends ViewManager
	{
		private var tableId:UInt64;

		public var playerViewList:Array;

		public var countDownList:Array;

		public function PlayerViewManager(tableId:UInt64, tableView:MovieClip)
		{
			super();
			this.tableId = tableId;

			tableView.addEventListener(Event.ADDED_TO_STAGE, onAddToStage);
			tableView.addEventListener(Event.REMOVED_FROM_STAGE, removeFromStage);

			playerViewList = [null, tableView.playerView_1, tableView.playerView_2, tableView.playerView_3, tableView.playerView_4, tableView.playerView_5, tableView.playerView_6];

			countDownList = [null, tableView.countDown_1, tableView.countDown_2, tableView.countDown_3, tableView.countDown_4, tableView.countDown_5, tableView.countDown_6,];
		}

		/**
		 * 保存本轮下注情况
		 **/
		private function get epicycleBetInfos():Array
		{
			return tableDataModel.epicycleBetInfos;
		}

		/**
		 * 桌子数据
		 **/
		public function get tableDataModel():TableDataModel
		{
			return TablesDataManager.tableDataModeDic[tableId.toString()];
		}

		private function onAddToStage(event:Event):void
		{
			//更新麻将桌子界面中显示
			dispatcher.addEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, updateViewData);

			//第一轮的要求押注请求
			dispatcher.addEventListener(MSProto.MSID_NEED_CALL_NTF.toString(), updateViewData);
			//要求客户端下注、过牌
			dispatcher.addEventListener(MSProto.MSID_NEED_BET_NTF.toString(), updateViewData);
			//用户跟牌广播
			dispatcher.addEventListener(MSProto.MSID_CALL_NTF.toString(), updateViewData);
			//用户加注广播
			dispatcher.addEventListener(MSProto.MSID_RAISE_NTF.toString(), updateViewData);
			//用户过牌广播
			dispatcher.addEventListener(MSProto.MSID_PASS_NTF.toString(), updateViewData);
			//用户押注广播
			dispatcher.addEventListener(MSProto.MSID_BET_NTF.toString(), updateViewData);
			//用户弃牌广播
			dispatcher.addEventListener(MSProto.MSID_FOLD_NTF.toString(), updateViewData);
			//赢牌的时候show自己的牌
			dispatcher.addEventListener(MSProto.MSID_SHOW_TILES_NTF.toString(), updateViewData);
			//其他玩家出牌的广播
			dispatcher.addEventListener(MSProto.MSID_DISCARD_NTF.toString(), updateViewData);
			//比赛结束10后清空桌面
			dispatcher.addEventListener(MajiangEvent.CLEAR_TABLE_INFO, onClearTableInfo);
			//广播更改sitout状态
			dispatcher.addEventListener(MSProto.MSID_SITOUT_NTF.toString(), updateViewData);

			dispatcher.addEventListener(MajiangEvent.CHIPS_FROMPOT_EFFECT_FINISH, onChipsFrompotEffectFinish);
		}

		protected function onClearTableInfo(event:GameEvent):void
		{
			if (tableId.toString() != event.data)
				return;
			updateViewData(new GameEvent(event.type, {tableId: tableId}));
		}

		private function removeFromStage(event:Event):void
		{
			//更新麻将桌子界面中显示
			dispatcher.removeEventListener(MajiangEvent.UPDATE_MAJIANG_TABLE_VIEW, updateViewData);

			//第一轮的要求押注请求
			dispatcher.removeEventListener(MSProto.MSID_NEED_CALL_NTF.toString(), updateViewData);
			//要求客户端下注、过牌
			dispatcher.removeEventListener(MSProto.MSID_NEED_BET_NTF.toString(), updateViewData);
			//用户跟牌广播
			dispatcher.removeEventListener(MSProto.MSID_CALL_NTF.toString(), updateViewData);
			//用户加注广播
			dispatcher.removeEventListener(MSProto.MSID_RAISE_NTF.toString(), updateViewData);
			//用户过牌广播
			dispatcher.removeEventListener(MSProto.MSID_PASS_NTF.toString(), updateViewData);
			//用户押注广播
			dispatcher.removeEventListener(MSProto.MSID_BET_NTF.toString(), updateViewData);
			//用户弃牌广播
			dispatcher.removeEventListener(MSProto.MSID_FOLD_NTF.toString(), updateViewData);
			//赢牌的时候show自己的牌
			dispatcher.removeEventListener(MSProto.MSID_SHOW_TILES_NTF.toString(), updateViewData);
			//其他玩家出牌的广播
			dispatcher.removeEventListener(MSProto.MSID_DISCARD_NTF.toString(), updateViewData);
			//比赛结束10后清空桌面
			dispatcher.removeEventListener(MajiangEvent.CLEAR_TABLE_INFO, onClearTableInfo);
			//广播更改sitout状态
			dispatcher.removeEventListener(MSProto.MSID_SITOUT_NTF.toString(), updateViewData);

			dispatcher.removeEventListener(MajiangEvent.CHIPS_FROMPOT_EFFECT_FINISH, onChipsFrompotEffectFinish);
		}

		private function updateViewData(event:GameEvent):void
		{
			if (event.data is String)
			{
				if (tableId.toString() != event.data)
					return;
			}
			else if (tableId.toString() != event.data.tableId.toString())
				return;

			var chairId:int;
			var tablePlayerDataModel:TablePlayerDataModel;
			for (var i:int = 1; i <= 6; i++)
			{
				var position:int = i;
				chairId = tableDataModel.getChairIdByPosition(position);
				tablePlayerDataModel = tableDataModel.seatedPlayers[chairId];
				var playerView:* = playerViewList[position];
				var countDown:* = countDownList[position];

				if (tablePlayerDataModel)
				{
					playerView.visible = true;
					countDown.visible = true;
					
					playerView.name_txt.text = tablePlayerDataModel.userName;
					if (event.type != MSProto.MSID_SHOW_TILES_NTF.toString())
					{
						playerView.money_txt.text = FloatHandler.TwoFormat(tablePlayerDataModel.money).toFixed(2);
					}

					playerView.action_txt.visible = false;
					playerView.name_txt.visible = !playerView.action_txt.visible;

					var isWin:Boolean = tableDataModel.isWin(tablePlayerDataModel.chairId);
					var isLose:Boolean = tableDataModel.isLose(tablePlayerDataModel.chairId);
					var isFold:Boolean = tableDataModel.isFold(tablePlayerDataModel.chairId);
					var isSitOut:Boolean = tableDataModel.isSitout(tablePlayerDataModel.chairId);

					//更新显示该玩家本轮下注情况
					var actionInfo:Object = epicycleBetInfos[chairId];

					if (isWin)
					{
						playerView.action_txt.text = ResourceManager.getInstance().getString("MahjongResource", "action_" + ActionType.WIN);
						playerView.action_txt.visible = true;
					}
					else if (isFold)
					{
						playerView.action_txt.text = ResourceManager.getInstance().getString("MahjongResource", "action_" + ActionType.FOLD);
						playerView.action_txt.visible = true;
					}
					else if (isLose)
					{
						playerView.action_txt.text = ResourceManager.getInstance().getString("MahjongResource", "action_" + ActionType.LOSE);
						playerView.action_txt.visible = true;
					}
					else if (isSitOut && tableDataModel.playerTiles[chairId].length < 1)
					{
						playerView.action_txt.text = ResourceManager.getInstance().getString("MahjongResource", "action_" + ActionType.SITOUT);
						playerView.action_txt.visible = true;
					}
					else if (actionInfo)
					{
						playerView.action_txt.text = ResourceManager.getInstance().getString("MahjongResource", "action_" + actionInfo.action);
						playerView.action_txt.visible = true;
					}

					if (tableDataModel.playerTiles[chairId].length < 1 && isSitOut == false)
					{
						playerView.action_txt.visible = false;
					}
				}
				else
				{
					playerView.visible = false;
					countDown.visible = false;
				}
				
				playerView.name_txt.visible = !playerView.action_txt.visible;
			}
		}



		private function onChipsFrompotEffectFinish(event:MajiangEvent):void
		{
			var chairId:int;
			var tablePlayerDataModel:TablePlayerDataModel;
			for (var i:int = 1; i <= 6; i++)
			{
				var position:int = i;
				chairId = tableDataModel.getChairIdByPosition(position);
				tablePlayerDataModel = tableDataModel.seatedPlayers[chairId];
				var playerView:* = playerViewList[position];
				var countDown:* = countDownList[position];

				if (tablePlayerDataModel)
				{
					playerView.money_txt.text = FloatHandler.TwoFormat(tablePlayerDataModel.money).toFixed(2);
				}
				else
				{
					playerView.visible = false;
					countDown.visible = false;
				}
			}
		}
	}
}
